package com.grade.service;

import com.grade.dao.CourseDao;
import com.grade.domain.Course;
import com.grade.domain.CourseDto;
import com.grade.domain.Response;
import com.grade.domain.StudentGradeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {

    @Autowired
    private CourseDao courseDao;

    public Response<List<CourseDto>> getCourseById(Integer id, String da) {
       return Response.success(courseDao.getCourseById(id,da));
    }

    public Response<List<StudentGradeDto>> getCourseGradeByTeacherId(Integer id,String courseName) {
        if(courseName==null||courseName.isEmpty()){
            return Response.success(courseDao.getCourseGradeByTeacherId(id));
        }
        return Response.success(courseDao.getCourseGradeByTeacherId2(id,courseName));

    }

    public Response<List<Course>> getCourseByTeacherId(Integer id) {
        return Response.success(courseDao.getCourseByTeacherId(id));
    }

    public Response<String> updateCourseGrade(Course course) {
        Integer a=courseDao.isExist(course);
        if(a==0){
            return Response.error("该学生没有该课程成绩");
        }
        courseDao.updateCourseGrade(a,course.getCourseGrade());
        return Response.success("修改成功");
    }

    public Response<String> addCourseGrade(Course course) {
        //判断是否存在课程学生的对应关系
        Integer a=courseDao.isExist(course);
        if(a==0){
            return Response.error("该学生没有该课程成绩");
        }
        //判断他是否有成绩
        Integer b=courseDao.isExistGrade(a);
        if(b!=0){
            return Response.error("该学生已经有该课程成绩");
        }
        courseDao.updateCourseGrade(a,course.getCourseGrade());
        return Response.success("添加成绩成功");
    }
}
